//
//  ViewController.m
//  01.CoreData的简单使用
//
//  Created by J on 15/11/7.
//  Copyright © 2015年 JZJ. All rights reserved.
//
#import <CoreData/CoreData.h>
#import "ViewController.h"
#import "Employee.h"
#import "Department.h"
@interface ViewController (){
    
    
    NSManagedObjectContext* _context;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

//1.创建模型文件[相当于数据库里的表]
//2.添加实体[一张表]
//3.创建实体类[相当模型]
//4.生成上下文 关联模型文件生成数据库
    /**
     关联的时候 如果本地没有数据库文件 CoreData自己会创建
     */
    

    //生成上下文
    NSManagedObjectContext* context=[[NSManagedObjectContext alloc]initWithConcurrencyType:0];//导入 #import <CoreData/CoreData.h>

    //上下文关联数据库 持久化存储调度器
    
    //持久化 将数据保存到一个文件 而不是内存
    //model模型文件
    NSManagedObjectModel* model=[NSManagedObjectModel mergedModelFromBundles:nil];
    
    NSPersistentStoreCoordinator* store=[[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:model];
    
    context.persistentStoreCoordinator=store;//上下文的一个属性 持久化调度器
    
    
    //告诉coreData 数据库的名字和路径
    
    
    NSString* doc=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString* sqlitePath=[doc stringByAppendingPathComponent:@"company.sqlite"];
    
    
    NSLog(@"%@",sqlitePath);
    
    [store addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:sqlitePath] options:nil error:nil];
    _context=context;//保存
    

}

//数据库的操作 CURD    Create Update Read Delete  增删改查


//增
- (IBAction)addEmplayee{
    
    //创建两个部门 iOS android
    Department* iOSDepart=[NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:_context];
    iOSDepart.name=@"iOS";
    iOSDepart.departNumber=@"0001";
    iOSDepart.createDate=[NSDate date];
    
    
    
    
    Department* androidDepart=[NSEntityDescription insertNewObjectForEntityForName:@"Department" inManagedObjectContext:_context];
    androidDepart.name=@"android";
    androidDepart.departNumber=@"0002";
    androidDepart.createDate=[NSDate date];
    
    
    
    //创建两个员工对象 iOS 张三   android李四
    
    Employee * zhangsan =[NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context];
    zhangsan.name=@"张三";
    zhangsan.height=@1.90;
    zhangsan.birthday=[NSDate date];
    zhangsan.department=iOSDepart;
    
    
    Employee * liSi =[NSEntityDescription insertNewObjectForEntityForName:@"Employee" inManagedObjectContext:_context];
    liSi.name=@"李四";
    liSi.height=@2.0;
    liSi.birthday=[NSDate date];
    liSi.department=androidDepart;//指定下所在部门  不然两个表之间就没有关联关系了
    
    
    //直接保存数据库
    NSError* error=nil;
    
    [_context save:&error];//由上下文来管理
    
    if (error) {
        NSLog(@"%@",error);
    }
    
    
}


//删
- (IBAction)deleteEmployee{
    
}



//改
- (IBAction)updateEmployee{
    
  }



//查
- (IBAction)readEmployee{
    
    //读取iOS部门的员工
    //1.FetchRequest 抓取请求对象
    NSFetchRequest * request=[NSFetchRequest fetchRequestWithEntityName:@"Employee"];
    
    //2.设置过滤条件
    //2.1查找张三
    NSPredicate *pre=[NSPredicate predicateWithFormat:@"department.name=%@",@"iOS"];//通过.语法员工里面的部门属性 然后 部门里面的name属性
    
    
    request.predicate=pre;// 将过滤条件加入到request中
    
    //4.执行请求
    
    
    
    NSError* error=nil;
    
    NSArray* result= [_context executeFetchRequest:request error:&error];//返回数组
    
    
    if (error) {
        NSLog(@"%@",error);
    }
    
    for (Employee* em in result) {
        
        
        NSLog(@"%@ %@",em.name,em.department.name);//员工里面的department属性里name属性
    }
    
    
    //    NSLog(@"%@",result);
    
    
    
}





- (IBAction)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
